import { Local } from '@/utils/storage';
import i18n from '@/language/index';
/**
 * @description: 验证账号
 * @param {any} rule
 * @param {any} value
 * @param {any} callback
 */
export const verifyUser = (rule: any, value: any, callback: any) => {
	if (value === '') {
		callback(new Error(i18n.global.t('error.account')));
	} else {
		const regex = /^\d{6,11}$/;
		if (regex.test(value)) {
			return callback();
		}
		callback(new Error(i18n.global.t('error.errAccount')));
	}
};
/**
 * @description: 验证密码
 * @param {any} rule
 * @param {any} value
 * @param {any} callback
 */
export const verifyPass = (rule: any, value: any, callback: any) => {
	if (value === '') {
		callback(new Error(i18n.global.t('error.password')));
	} else {
		const password = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/;
		if (password.test(value)) {
			return callback();
		}
		callback(new Error(i18n.global.t('error.errPassword')));
	}
};
/**
 * @description: 验证验证码
 * @param {any} rule
 * @param {any} value
 * @param {any} callback
 */
export const verifyCode = (rule: any, value: any, callback: any) => {
	if (value === '') {
		callback(new Error(i18n.global.t('error.code')));
	} else {
		if (value.length == 4) {
			return callback();
		}
		callback(new Error(i18n.global.t('error.errCode')));
	}
};
/**
 * 缓存账号和密码
 * @param obj
 */
export const addStorage = (obj: { username: string; password: string }) => {
	Local.set<{
		username: string;
		password: string;
	}>('user', obj);
};
